package com.lanswon.qzsmk.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.itextpdf.text.log.SysoCounter;
import com.lanswon.qzsmk.exception.SystemException;
import com.lanswon.qzsmk.model.CardBatch;

public class SequenceNumberUtil {
	private static int certSeq=0;
	private static int busSeq=0;

	private static Logger logger=(Logger) LoggerFactory.getLogger(SequenceNumberUtil.class);
	
	/**
	 * 生成任务编号
	 * @param cardPcode
	 * @param previousNumber
	 * @return
	 */
	public static String getSequenceNumber(String cardPcode,String previousNumber){
		String lastNo=null;
		if(previousNumber==null)
			lastNo="0000";
		else
			lastNo=getlastNo(previousNumber);
		int a=Integer.valueOf(lastNo);
		a++;
		String aa=String.valueOf(a);
		StringBuffer sbBuffer=new StringBuffer();
		sbBuffer.append(cardPcode).append(new SimpleDateFormat("yyyyMMdd").format(new Date())).append(CitizenCardNoGeneratorUtil.generatorSeq(aa, lastNo.length()));
		return sbBuffer.toString();
	}
	
	
	private  static String getlastNo(String previousNumber){
		
		return previousNumber.substring(previousNumber.length()-4);
	}
	
	/**
	 * 生成流水号
	 */
	public static String generatorBusinessNo(){

//		String orderNo = "" ;
//		UUID uuid = UUID.randomUUID();
//	    String sdf = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
//	    orderNo = uuid.toString().substring(0, 8);
//	    orderNo = orderNo + sdf ;
//	    return orderNo ;
//		return uuid.toString().replace("-", "");
		String string=DateFormatUtils.format(new Date(), "yyMMdd");
		String seq=getBusSeq();
		return string+seq;
		

	}
	
	
	public static synchronized String getBusSeq() {
		SequenceNumberUtil.busSeq++;
		return CommonUtil.patchHexString(String.valueOf(busSeq), 6);
	}


	public static void setBusSeq(int busSeq) {
		SequenceNumberUtil.busSeq = busSeq;
	}


	public static synchronized String getCertSeq(){
		SequenceNumberUtil.certSeq++;
		return CommonUtil.patchHexString(String.valueOf(certSeq), 6);
		
	}
	
	public static void setCertSeq(int certSeq){
		SequenceNumberUtil.certSeq=certSeq;
	}
	
	/**
	 * 生成凭证号
	 * @return
	 */
	public static String generatorCertNo(){
		String string=DateFormatUtils.format(new Date(), "yyMMdd");
		String seq=getCertSeq();
		return "PZ"+string+seq;
	}
	
	
	/**
	 * 获得指定日期当天零时零分零秒
	 * @return
	 */
	public static Long initDateByDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 24);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTimeInMillis();
	}

	
	
	/**
	 * 生成制卡批次号
	 * @param cb
	 * @return
	 */
	public static String generatorCardBatchKey(CardBatch cb){
		Long preLong = 0L;
		Long nowLong = 0L;
		String start = DateFormatUtils.format(new Date(), "yyMMdd");
		if(cb != null){
			String preDate = cb.getBatchMarkerNo().substring(0, 6);
			Date date = new Date();
			String aString=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);	
			try {
				nowLong = initDateByDay(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(aString));
				 Date date2 = DateUtils.parseDate(preDate, "yyMMdd");
				 preLong = initDateByDay(date2);
			} catch (ParseException e) {
				e.printStackTrace();
				 String msg1="日期转换失败";
					logger.error(msg1);
					throw new SystemException(SystemException.ParseException, msg1);

			}
			 
			
		}
		
		
		
		String batchMarkerNo = "";
		
		if(cb == null || preLong < nowLong){
			batchMarkerNo = start+"0001";
		}else{
			String preEnd = cb.getBatchMarkerNo().substring(6);
			Integer preEnd1 = Integer.valueOf(preEnd);
			Integer curEnd  = preEnd1+1;
			String curEnd1 = curEnd.toString();
			if(curEnd1.length()==4){
				batchMarkerNo = start+curEnd1;
			}else if(curEnd1.length()==3){
				batchMarkerNo = start+"0"+curEnd1;
			}else if(curEnd1.length()==2){
				batchMarkerNo = start+"00"+curEnd1;
			}else if(curEnd1.length()==1){
				batchMarkerNo = start+"000"+curEnd1;
			}
			
		}
		
		return batchMarkerNo;
	}
	
	
	public static void main(String[] args) throws Exception {
//		for(int i=0;i<10;i++){
//			String str1=generatorBusinessNo();
//			String str2=generatorBusinessNo();
//			System.out.println(str1);
//			System.out.println(str1.equals(str2)+"===>第"+i+"次循环");
//			if(str1.equals(str2)){
//				break;
//			}
//		}
//		for(int i=0;i<100;i++){
//			String a=getCertSeq();
//			String b=getCertSeq();
//			System.out.println(a+"--"+b);
//			System.out.println(a.equals(b));
//		}
		Long a=1541649600963L;
		Long b=1541606400000L;
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(a)));
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(b)));
		String aString="2018-11-08 17:09:44";
		String bString="2018-11-08 00:00:00";
		System.out.println(initDateByDay(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(aString)));
		System.out.println(initDateByDay(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(bString)));
		String aaa="181115000001";
		System.out.println(aaa.substring(6));
	}
}

